﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;
using System.IO;

namespace NewsMine.DataManagers
{
    public class FaceBookPageManager
    {
        public static List<FacebookPage> GetFacebookPageInfo()
        {
            object objFacebookAccounts = NewsMine.Storage.Defaults.SettingsStore.Get(SettingsManager.FacebookAccountsKey);

              if (objFacebookAccounts == null)
              {
                  new List<FacebookAccount>();
              }

              if (objFacebookAccounts.GetType() != typeof(FacebookAccount))
              {
                  new List<FacebookAccount>();
              }

              FacebookAccount fbAccount = (FacebookAccount)objFacebookAccounts;

              if (fbAccount.PageInformation != null)

                  return fbAccount.PageInformation;
              else

                  return new List<FacebookPage>();
        }

        public static List<FacebookPageInfo> GetAllFaceBookPagesInfo()
        {
            List<FacebookPageInfo> fbPageInfoList = new List<FacebookPageInfo>();

            //fbPageInfoList.Add(new FacebookPageInfo { Name = "Look at Telugu News", PostingSecretEmailAddress = "aryan650doter@m.facebook.com" });

            var fileName = Path.Combine(Configuration.ConfigurationManager.StorageBaseFolder, "FacebookPageConfig.txt");

            if (File.Exists(fileName) == false)
            {
                return StaticPageData();

            }
            else
            {

                fbPageInfoList = (from line in File.ReadAllLines(fileName)
                                  let columns = line.Split(',')
                                  select new FacebookPageInfo
                          {
                              Name = columns[0],
                              PostingSecretEmailAddress = columns[1]
                          }).ToList<FacebookPageInfo>();


                //todo: this list needs to come from the csv file. Yet to be implemted.

                return fbPageInfoList;
            }

        }

        private static List<FacebookPageInfo> StaticPageData()
        {
            List<FacebookPageInfo> pageInfoList = new List<FacebookPageInfo>();

            pageInfoList.Add(new FacebookPageInfo("Telugu News","aryan650doter@m.facebook.com"));
            pageInfoList.Add(new FacebookPageInfo("tamil news", "husk414robust@m.facebook.com"));
            pageInfoList.Add(new FacebookPageInfo("Kanada News", "latent680nones@m.facebook.com"));
            pageInfoList.Add(new FacebookPageInfo("Marathi News", "cupric379vacuum@m.facebook.com"));
            pageInfoList.Add(new FacebookPageInfo("Hindi News", "sterna724urdu@m.facebook.com"));
            pageInfoList.Add(new FacebookPageInfo("Malayalam News", "holdup846regard@m.facebook.com"));

            return pageInfoList;
        }

        static List<GmailUserForSMTP>  smtpUsers = new List<GmailUserForSMTP>();


        public static List<GmailUserForSMTP> GetListOfGmailUsersForSMTPUsage()
        {
            if (smtpUsers.Count > 0)
                return smtpUsers;

            smtpUsers.Add(new GmailUserForSMTP("aatmaja@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("abhaya@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("Abhinaya@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("adithi@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("aditi@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("adwitiya@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("ahalya@lookatnews.com", "Shiv@H@num@n321a"));
            //smtpUsers.Add(new GmailUserForSMTP("aishwarya@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("akilaha@lookatnews.com", "Shiv@H@num@n321a"));
            smtpUsers.Add(new GmailUserForSMTP("akruthi@lookatnews.com", "Shiv@H@num@n321a"));

            return smtpUsers;
        }

        public static GmailUserForSMTP GetNextGmailAddressToUseInSMTP()
        {
            var emailList = GetListOfGmailUsersForSMTPUsage();

            for (int i = 0; i < emailList.Count; i++)
            {
                if ((DateTime.UtcNow - emailList[i].StartTime) > TimeSpan.FromMinutes(24 * 60))
                {
                    emailList[i].MailSentCount = 0;
                    emailList[i].StartTime = DateTime.UtcNow;
                }
            }

            var result = (from mail in emailList
                          orderby mail.MailSentCount ascending
                          select mail).FirstOrDefault<GmailUserForSMTP>();

            if (result != null)
            {
                result.MailSentCount++;
                if (result.StartTime == DateTime.MinValue)
                    result.StartTime = DateTime.UtcNow;

                result.EndTime = DateTime.UtcNow;
            }

            if (result.MailSentCount >= 100)
            {
                throw new ApplicationException("Gmail day limit is exceeded for all the mail addresses");
            }

            return result;
        }
    }


    public class GmailUserForSMTP
    {
        public GmailUserForSMTP()
        {
        }

        public GmailUserForSMTP(string email, string psw)
        {
            GmailUserName = email;
            GmailPassword = psw;
        }

        public string GmailUserName { get; set; }
        public string GmailPassword { get; set; }
        public int MailSentCount { get; set; }

        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
    }

}
